package com.xquant.trade.report.controller;

import com.xquant.common.core.constant.OpexMonitorConstant;
import com.xquant.common.core.exception.ServiceException;
import com.xquant.trade.utils.ResponseVo;
import com.xquant.trade.report.entity.OpexMonitor;
import com.xquant.trade.report.entity.OpexMonitorDetail;
import com.xquant.trade.report.service.OpexMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.Map;

import static com.xquant.common.core.utils.PageUtils.startPage;

/**
 * @Auther:
 * @Date:
 * @Description: newCloud Controller  运维监控
 */
@RestController
@RequestMapping("opexMonitor")
public class OpexMonitorController {

    @Autowired
    private OpexMonitorService opexMonitorService;

    @ResponseBody
    @RequestMapping(value = "/checkOpexMonitor.action")
    @PreAuthorize("@ss.hasPermi('report:opexMonitor:check')")
    public Object checkOpexMonitor(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        startPage();
        return opexMonitorService.checkOpexMonitor(params);
    }

    /**
     * 单个检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkOpexMonitorByOpexType.action")
    @PreAuthorize("@ss.hasPermi('report:opexMonitor:checkOpexMonitorByOpexType')")
    public Object checkOpexMonitorByOpexType(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        switch ((String) params.get("opexType")){
            case OpexMonitorConstant
                    .OPEX_TYPE.DAILYPRICE:
                return checkDailyPrice(request, params);
            case OpexMonitorConstant
                    .OPEX_TYPE.IR_CURVE:
                return checkIrCurve(request, params);
            case OpexMonitorConstant
                    .OPEX_TYPE.DIV_CURVE:
                return checkDivCurve(request, params);
            case OpexMonitorConstant
                    .OPEX_TYPE.VOL_SURFACE:
                return checkVlsCurve(request, params);
            case OpexMonitorConstant
                    .OPEX_TYPE.SUMMIT:
                return checkSummit(request, params);
            case OpexMonitorConstant
                    .OPEX_TYPE.INIT_PRICE:
                return checkInitPrice(request, params);
            case OpexMonitorConstant
                    .OPEX_TYPE.END_DAY_VALUATION:
                return checkCalc(request, params);
            default:
                break;
        }
        return ResponseVo.failure("未支持该类型的检查");
    }

    /**
     * 行情检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkDailyPrice.action")
    public Object checkDailyPrice(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        opexMonitorService.checkDailyPrice(params);
        return ResponseVo.success();
    }

    /**
     * 曲线曲面rates检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkCurve.action")
    public Object checkCurve(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        startPage();
        opexMonitorService.checkCurve(params);
        return ResponseVo.success();
    }

    /**
     * 利率曲线rates检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkIrCurve.action")
    public Object checkIrCurve(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        ResponseVo responseVo = new ResponseVo();
        try {
            opexMonitorService.checkIrCurve(params);
            responseVo.setErrorCode(0);
        } catch (SQLException e) {
            responseVo.setErrorCode(-1);
            responseVo.setErrorMsg("检查失败!");
            throw new ServiceException("检查失败");
        }
        return responseVo;
    }

    /**
     * 红利曲线rates检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkDivCurve.action")
    public Object checkDivCurve(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        ResponseVo responseVo = new ResponseVo();
        try {
            opexMonitorService.checkDivCurve(params);
            responseVo.setErrorCode(0);
        } catch (SQLException e) {
            responseVo.setErrorCode(-1);
            responseVo.setErrorMsg("检查失败!");
            throw new ServiceException("检查失败");
        }
        return responseVo;
    }

    /**
     * 波动率曲面rates检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkVlsCurve.action")
    public Object checkVlsCurve(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        ResponseVo responseVo = new ResponseVo();
        try {
            opexMonitorService.checkVlsCurve(params);
            responseVo.setErrorCode(0);
        } catch (SQLException e) {
            responseVo.setErrorCode(-1);
            responseVo.setErrorMsg("检查失败!");
            throw new ServiceException("检查失败");
        }
        return responseVo;
    }

    /**
     * summit导入检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkSummit.action")
    public Object checkSummit(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        return opexMonitorService.checkSummitFile(params);
    }

    /**
     * 期初价格检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkInitPrice.action")
    public Object checkInitPrice(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        return opexMonitorService.checkSpotPrice(params);
    }

    /**
     * 日终估值检查
     */
    @ResponseBody
    @RequestMapping(value = "/checkCalc.action")
    public Object checkCalc(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        return opexMonitorService.checkCalc(params);
    }

    @ResponseBody
    @RequestMapping(value = "/queryOpexMonitorDetailList.action")
    @PreAuthorize("@ss.hasPermi('report:opexMonitorDetail:list')")
    public Object queryOpexMonitorDetailList(OpexMonitorDetail params) {
        startPage();
        return opexMonitorService.queryOpexMonitorDetailList(params);
    }

    @ResponseBody
    @RequestMapping(value = "/queryOpexMonitorList.action")
    @PreAuthorize("@ss.hasPermi('report:opexMonitor:list')")
    public Object queryOpexMonitorList(OpexMonitor params) {
        startPage();
        return opexMonitorService.queryOpexMonitorList(params);
    }

    //下载压缩SUMMIT文件的zip文件
    @RequestMapping("/downloadFileZip.action")
    @PreAuthorize("@ss.hasPermi('report:opexMonitorDetail:downloadFileZip')")
    public void downloadFileZip(OpexMonitorDetail params, HttpServletResponse response) {
        opexMonitorService.downloadFileZip(params, response);
    }

    //下载单个的SUMMIT文件
    @RequestMapping("/downloadFile.action")
    @PreAuthorize("@ss.hasPermi('report:opexMonitorDetail:download')")
    public void downloadFile(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        opexMonitorService.downloadFile(params, response);
    }
}
